/*****************************************************************************************
**  Author:jxx 2022
**  QQ:283591387
**完整文档见：http://v2.volcore.xyz/document/api 【代码生成页面ViewGrid】
**常用示例见：http://v2.volcore.xyz/document/vueDev
**后台操作见：http://v2.volcore.xyz/document/netCoreDev
*****************************************************************************************/
//此js文件是用来自定义扩展业务代码，可以扩展一些自定义页面或者重新配置生成的代码
import modelHeader from "../goodsin_class/goodsInCom.vue"
import gridHeader from "@/components/print/selectTemplet.vue"
// import { mapMutations } from "vuex";
let extension = {
  components: {
    //查询界面扩展组件
    gridHeader,
    gridBody: '',
    gridFooter: '',
    //新建、编辑弹出框扩展组件
    modelHeader: modelHeader,
    modelBody: '',
    modelFooter: ''
  },
  tableAction: '', //指定某张表的权限(这里填写表名,默认不用填写)
  buttons: { view: [], box: [], detail: [] }, //扩展的按钮
  methods: {
    // ...mapMutations(
    //   ("salesContractMX", ["storeRemoveForm", "storeChangeForm"])
    // ),
    //下面这些方法可以保留也可以删除
    async onInit() {
      this.queryFields=['WHSE_name','CreateDate']
      this.textInline = false
      this.detailOptions.textInline = false
      let startTime = await this.base.getOneWeekAgoDate()
      let endTime = await this.base.getDate(false) + " 23:59:59"
      this.searchFormFields.CreateDate = [startTime, endTime]
      if (this.$route.path == "/q_warehouse_location" && JSON.stringify(this.$route.query) != '{}') {
        console.log("触发查询")
        await this.base.setSearchParamFn(this)
        // this.searchFormFields.WHSE_no = null
        // this.searchFormFields.WHSE_name = null
        // this.searchFormFields.CreateDate = [null, null]
        setTimeout(() => {
          this.refresh()
        })
      }
    },
    async onInited() {
      this.single = true
      // this.setFiexdSearchForm(true);
      // this.buttons.forEach(item => {
      //   if (item.name == "高级查询") {
      //     item.hidden = true
      //   }
      // })
      // this.detailOptions.columns.forEach(column => {

      // });
      await this.detailOptions.buttons.splice(this.detailOptions.buttons.findIndex(item => item.name == "添加行"), 1)
      this.detailOptions.buttons.unshift({
        name: "选择商品",//按钮名称
        icon: 'el-icon-plus',//按钮图标，参照iview图标
        type: 'primary',
        plain: true,
        onClick: function () { //触发事件
          //   let form = {
          //     AuditStatus: 1
          // }
          // this.storeChangeForm(form);
          this.$store.commit("setTabelText", true)
          this.$refs.modelHeader.detailOpen()
        }
      })
      // //明细表点击表格时触发编辑方法
      this.detailOptions.beginEdit = (row, column, index) => {
        if (this.$route.path != "/q_warehouse_location") { //|| !this.CreateUserBoole|| this.rowAuditStatus != 0 
          return false
        }
        let rows = document.querySelectorAll(".grid-detail .el-table__body .el-table__row")
        setTimeout(() => {
          let testInputDom = rows[index].querySelectorAll("td")[3].querySelector("input")
          if (testInputDom) {
            testInputDom.addEventListener("keyup", (e) => {
              if (e.key == "Enter") {
                let url = `api/q_goodsInList_class/getGoosData?dm=${testInputDom.value}`
                this.http.post(url, {}).then(reslut => {
                  let data = reslut[0]
                  row.Item_name = data.Item_name
                  row.Item_sub = data.Item_sub
                }).catch(error => {
                  row.Item_name = null
                  row.Item_sub = null
                  this.$message.error("暂无此编码商品信息")
                })
              }
            })
          }
        }, 0)
        return true;//返回false不会进行编辑
      }
    },
    loadTreeChildren(tree, treeNode, resolve) {
      let url = `api/q_warehouse_location/getTreeTableChildrenData?class_Id=${tree.WHSE_Id}`;
      this.http.post(url, {}).then((result) => { resolve(result.rows); });
    },
  async  searchBefore(param) {
      //界面查询前,可以给param.wheres添加查询参数
      //返回false，则不会执行查询
      try {
        if (this.$route.path == "/q_warehouse_location" && JSON.stringify(this.$route.query) != '{}') {
          await this.base.setSearchBefore(this, "Location_id",param)
          // const sectionState = ["[null,null]",JSON.stringify(["",null]),JSON.stringify([null,""]),JSON.stringify(["",""])]
          // const dateState=sectionState.includes(JSON.stringify(this.searchFormFields.CreateDate))
          // if (this.searchFormFields.WHSE_no || this.searchFormFields.WHSE_name ||!dateState) {
          //   param.wheres=param.wheres.filter(item=>item.name!="Location_id")
          //   this.$route.query = {}
          // }
        }
      } catch (error) {
        console.log("error",error)
      }
      return true;
    },
    searchAfter(result) {
      //查询后，result返回的查询数据,可以在显示到表格前处理表格的值
      return true;
    },
    addBefore(formData) {
      //新建保存前formData为对象，包括明细表，可以给给表单设置值，自己输出看formData的值
      return true;
    },
    async addBeforeAsync(formData) {
      let detailRowData = this.$refs.detail.rowData
      // formData.mainData.Item_num = formData.detailData.length
      await formData.detailData.forEach(item => {
        item.WHSE_name = formData.mainData.WHSE_name
        item.WHSE_no = formData.mainData.WHSE_no
      })
      return true
    },
    updateBefore(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      return true;
    },
    async updateBeforeAsync(formData) {
      //编辑保存前formData为对象，包括明细表、删除行的Id
      let detailRowData = this.$refs.detail.rowData
      // formData.mainData.Item_num = formData.detailData.length
      await formData.detailData.forEach(item => {
        item.WHSE_name = formData.mainData.WHSE_name
        item.WHSE_no = formData.mainData.WHSE_no
      })
      return true
    },
    rowClick({ row, column, event }) {
      //查询界面点击行事件
      this.$refs.table.$refs.table.toggleRowSelection(row); //单击行时选中当前行;
    },
    detailRowClick({ row, column, event }) {
      this.$refs.detail.$refs.table.toggleRowSelection(row);
      // if (this.$route.path != "/q_warehouse_location" || this.rowAuditStatus != 0 || !this.CreateUserBoole) { return }
      if (column.property != "WHSE_no" && column.property != "WHSE_name" && column.property != undefined) {
        let dom = event.target
        if (dom.nodeName != "SPAN") {
          setTimeout(() => {
            let inpDom = dom.querySelector("input")
            inpDom.focus()
          }, 0)
        }
      }
    },
    onModelClose(iconClick) {
      //iconClick=true为点击左中上角X触发的关闭事件
      //如果返回 false不会关闭弹出框 
      //return false;
      // this.storeRemoveForm()
      this.$store.commit("setTabelText", true)
      this.boxModel = false;
    },
    async modelOpenBefore(row) {
      this.$store.commit("setTabelText", false)
      this.CreateUserBoole = row.CreateID == this.$store.state.userID
      // this.rowAuditStatus = await row.AuditStatus
      if (JSON.stringify(row) == "{}") {
        // this.rowAuditStatus = 0
        this.CreateUserBoole = true
      }
      if (this.$route.path != "/q_warehouse_location") { // || !this.CreateUserBoole|| this.rowAuditStatus != 0 
        this.boxButtons[0].hidden = true
        this.detailOptions.buttons.forEach(item => {
          if (item.name != "导出" && item.name != "刷新") {
            item.hidden = true
          }
        })
      } else {
        this.boxButtons[0].hidden = false
        this.detailOptions.buttons.forEach(item => item.hidden = false)
      }
    },
    modelOpenAfter(row) {
      this.editFormOptions.forEach(option => {
        option.forEach(item => {

          if (this.currentAction == "Add") {
            if (item.field == "WHSE_name") {

              // item.remote = true
              item.extra = {
                icon: "el-icon-zoom-in",
                text: "选择仓库",
                style: "color:#3a8ee6;font-size:13px;cursor:default",
                click: () => {
                  this.$store.commit("setTabelText", true)
                  this.$refs.modelHeader.detailWarehouseOpen()
                }
              }
            }
          } else {
            if (item.field == "WHSE_name") {
              // item.remote = true
              item.extra = null
            }
          }
          if (this.$route.path != "/q_warehouse_location") { //|| !this.CreateUserBoole|| this.rowAuditStatus != 0 
            item.disabled = true
            if (item.field == "WHSE_name") {
              item.extra = null
            }
          }
        })
      })

    },
    delBefore(ids, rows) { //查询界面的表删除前 ids为删除的id数组,rows删除的行
      let selectRows = this.getSelectRows()
      let off = selectRows.findIndex(item => item.Item_num != 0 && item.Item_num) != "-1"
      if (off) {
        this.$message.warning("不能删除存放商品的库位")
        return false
      } else {
        return true
      }
    },
    //删除库位
    deleteWHSELocationFn() {
      console.log("删除");
      console.log("选中的行", this.getSelectRows());
      let selectRows = this.getSelectRows()
      console.log("index", selectRows.findIndex(item => item.Item_num != 0));
      if (selectRows.findIndex(item => item.Item_num != 0) != "-1") {
        this.$message.warning("不能删除存放商品的库位")
        return false
      } else {
        return true
      }

    }
  }
};
export default extension;
